#include <iostream>
#include <fstream>

///////////////////////15-动态二维数组操作 ////////////////////////
class DynamicArray {
public:
		DynamicArray(int r, int c);
		void InputDta();
		double Mean();
		void SaveTxt();
		~DynamicArray();

private:
		int row_;
		int column_;
		int **p_;
		double mean_;
};

DynamicArray::DynamicArray(int r, int c) : row_(r), column_(c) {
		p_ = new int *[row_];
		for (int i = 0; i < column_; ++i) {
				p_[i] = new int;
		}
		std::cout << "资源分配完毕" << std::endl;
}
void DynamicArray::InputDta() {
		int arrary_num = row_ * column_;
		int tmp = 0;
		while (arrary_num--) {
				std::cout << "输入第" << row_ * column_ - arrary_num << "/"
				          << row_ * column_ << "个数:";
				std::cin >> tmp;
				int i = (row_ * column_ - arrary_num - 1) / column_;
				int j = (row_ * column_ - arrary_num - 1) % column_;
				p_[i][j] = tmp;
				//    std::cout<<"["<<i<<"]"<<"["<<j<<"]="<<tmp<<std::endl;
		}
}

double DynamicArray::Mean() {
		int sum = 0;
		for (int i = 0; i < row_; ++i) {
				for (int j = 0; j < column_; ++j) {
						sum += p_[i][j];
				}
		}
		mean_ = sum / 1.0 / (row_ * column_);
		std::cout << "平均数为:" << mean_ << std::endl;
		return mean_;
}
void DynamicArray::SaveTxt() {
		std::ofstream file("./data.txt", std::ios::app);
		file << "输入的" << row_ * column_ << "个数据为:";
		for (int i = 0; i < row_; ++i) {
				for (int j = 0; j < column_; ++j) {
						file << p_[i][j] << " ";
				}
		}
		file << ",数据平均值为:" << mean_ << "\n";
		file.close();
		std::cout << "数据写入完毕" << std::endl;
}
DynamicArray::~DynamicArray() {
		for (int i = 0; i < column_; ++i) {
				delete[] p_[i];
		}
		delete p_;
		std::cout << "资源已释放" << std::endl;
}

int main() {
		DynamicArray arr(2, 3);
		arr.InputDta();
		arr.Mean();
		arr.SaveTxt();

		return 0;
}